﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EShop.BussinessObjects;
using EShop.DataAccess;

namespace EShop.Common
{

    /// <summary> 
    /// Summary description for CGlobalData
    /// </summary>
    public class CGlobalData
    {
        private static User CurrentUser;
        private static Company CurrentCompany;
        private const string CurrentCompanyName = "Company1";

        public CGlobalData()
        {
            CurrentCompany=new Company();
            CurrentCompany.Name = CurrentCompanyName;
            CurrentCompany = CDACompany.SelectByName(CurrentCompany)[0];
        }

        public static int GetCurrentCompanyID()
        {
            //Company _company = GetCurrentCompany();
            if (CurrentCompany != null)
                return CurrentCompany.ID;
            return Convert.ToInt32(cnstDatabase.DBOperationResult.NullValue);
        }

        public static Company GetCurrentCompany()
        {
            //Company _company_Filter = new Company();
            //CompanyAddress _companyAddress_Filter = new CompanyAddress();
            //_companyAddress_Filter.UrldOmainAddress = HttpContext.Current.Request.Url.Host;
            //_company_Filter.CompanyAddress.Add(_companyAddress_Filter);
            //_company_Filter.ID = (int)cnstDatabase.DBOperationResult.ValueIsNotEffective;
            //List<Company> _companyList = DataAccess.CDACompany.Select(_company_Filter);
            //if (_companyList.Count > 0)
            //    return _companyList.First();
            //return null;
            return CurrentCompany;
        }

        public static void SetGlobalData(User user)
        {
            //HttpContext.Current.Session["CurrentUser"] = user;
            //HttpContext.Current.Session["glob"] = user.ID + "," +
            //    user.Contact.FirstName + "," +
            //    user.SecurityLevel + "," +
            //    user.Contact.ID + "," +
            //    user.LoginID;
            CurrentUser=new User();
            CurrentUser = user;
        }

        public static User GetCurrentUser()
        {
            //return (User)HttpContext.Current.Session["CurrentUser"];
            return CurrentUser;
        }

        public static string GetCurrentLoginID()
        {
            //return HttpContext.Current.Session["glob"].ToString().Split(',')[4];
            return CurrentUser.LoginID;
        }

        public static int GetCurrentUserID()
        {
            //return Convert.ToInt32(HttpContext.Current.Session["glob"].ToString().Split(',')[0]);
            return CurrentUser.ID;
        }

        public static string GetCurrentFirstName()
        {
            //return HttpContext.Current.Session["glob"].ToString().Split(',')[1];
            return CurrentUser.Contact.FirstName;
        }

        public static int GetCurrentContactID()
        {
            //return Convert.ToInt32(HttpContext.Current.Session["glob"].ToString().Split(',')[3]);
            return CurrentUser.Contact.ID;
        }

        public static bool IsLoggedIn()
        {
            return true;// !(HttpContext.Current.Session["glob"] == null);
        }

        public static void LogOut()
        {

            //HttpContext.Current.Session["glob"] = null;
        }

    }
}
